package com.fancy.learncenter.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.fancy.learncenter.R;
import com.fancy.learncenter.bean.BaseBean;
import com.fancy.learncenter.bean.CartoonTeacherWorkListDataBean;
import com.fancy.learncenter.common.JumpIntentKey;
import com.fancy.learncenter.net.CustomNetCallBack;
import com.fancy.learncenter.net.CustomNetSubscriber;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.ui.activity.base.BaseActivity;
import com.fancy.learncenter.ui.adapter.CartoonTeacherClassDetailAdapter;
import com.fancy.learncenter.utils.NetUtil;
import com.fancy.learncenter.utils.ToastUtil;
import com.fancy.learncenter.utils.Utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.ResponseBody;
import retrofit2.Response;

public class CartoonTeacherClassDetailActivity extends BaseActivity {

    public static String CLASS_PIC_KEY = "CLASS_PIC_KEY";
    public static String CLASS_NAME_KEY = "CLASS_NAME_KEY";
    public static String CLASS_NUM_KEY = "CLASS_NUM_KEY";
    public static String CLASS_ID_KEY = "CLASS_ID_KEY";
    public static String CLASS_STUDENT_NUM_KEY = "CLASS_STUDENT_NUM_KEY";
    CartoonTeacherClassDetailAdapter cartoonTeacherClassDetailAdapter;

    ArrayList<CartoonTeacherWorkListDataBean.ResultBean> itemDatas;

    View topView;

    TextView name;
    TextView school_num;
    TextView work_num;
    Button role_bt;
    Button report_bt;
    Button homework;
    SimpleDraweeView class_pic;

    private String classPic;
    private String className;
    private String classNum;
    private String classId;

    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;


    LinearLayout everyday_speak;


    int studentNum = 100;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cartoon_teacher_class_detail, Status.STATUS);
        ButterKnife.bind(this);
        initToolbar("班级详情");

        classId = getIntent().getStringExtra(CLASS_ID_KEY);
        classPic = getIntent().getStringExtra(CLASS_PIC_KEY);
        className = getIntent().getStringExtra(CLASS_NAME_KEY);
        studentNum = getIntent().getIntExtra(CLASS_STUDENT_NUM_KEY, 100);

        classNum = getIntent().getStringExtra(CLASS_NUM_KEY);



        topView = LayoutInflater.from(this).inflate(R.layout.activity_cartoon_teacher_class_detail_top, null);

        class_pic = (SimpleDraweeView) topView.findViewById(R.id.class_pic);
        name = (TextView) topView.findViewById(R.id.name);
        school_num = (TextView) topView.findViewById(R.id.school_num);

        class_pic.setImageURI(classPic);
        class_pic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(CartoonTeacherClassDetailActivity.this, CartoonEditClassActivity.class);
                intent.putExtra(CLASS_PIC_KEY, classPic);
                intent.putExtra(CLASS_ID_KEY, classId);
                intent.putExtra(CLASS_NAME_KEY, className);
                intent.putExtra(CLASS_STUDENT_NUM_KEY, studentNum);
                startActivity(intent);
            }
        });


        name.setText(className);
        school_num.setText(classNum);

        work_num = topView.findViewById(R.id.work_num);

        role_bt = (Button) topView.findViewById(R.id.role_bt);
        role_bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(CartoonTeacherClassDetailActivity.this, CartoonTeacherSudentActivity.class);
                intent.putExtra(CLASS_ID_KEY, classId);
                intent.putExtra(CLASS_STUDENT_NUM_KEY, studentNum);
                startActivity(intent);
            }
        });

        report_bt = (Button) topView.findViewById(R.id.report_bt);
        report_bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(CartoonTeacherClassDetailActivity.this, CartoonTeacherClassReportActivity.class);
                intent.putExtra(CLASS_ID_KEY, classId);
                startActivity(intent);
            }
        });

        homework = (Button) topView.findViewById(R.id.homework);
        homework.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (stuNum > 0) {
                    Intent intent = new Intent(CartoonTeacherClassDetailActivity.this, CartoonAssignHomeWorkActivity.class);
                    intent.putExtra(JumpIntentKey.CLASSID, classId);
                    intent.putExtra("className", className);
                    startActivity(intent);
                } else {
                    ToastUtil.show("请先添加学生");
                }

            }
        });

        everyday_speak = topView.findViewById(R.id.everyday_speak);
        everyday_speak.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(CartoonTeacherClassDetailActivity.this, CartoonTeacherEveryDayActivity.class);
                intent.putExtra(CartoonTeacherClassDetailActivity.CLASS_ID_KEY, classId);
                startActivity(intent);
            }
        });


        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);

        recyclerView.setLayoutManager(linearLayoutManager);
        itemDatas = new ArrayList<>();

        cartoonTeacherClassDetailAdapter = new CartoonTeacherClassDetailAdapter(this, itemDatas, classId, className);
        recyclerView.setAdapter(cartoonTeacherClassDetailAdapter);

        cartoonTeacherClassDetailAdapter.setHeaderView(topView, recyclerView);


    }

    @Override
    protected void onResume() {
        super.onResume();
        getTeacherClassList();
    }

    int stuNum = 0;

    private void getTeacherClassList() {
        Map<String, Object> map = new HashMap<>();
        map.put("classId", classId);

        HttpMehtod.getInstance().getListWork(map, new CustomNetSubscriber(this, true) {
            @Override
            public void onNext(Response<ResponseBody> responseBodyResponse) {


                BaseBean<CartoonTeacherWorkListDataBean> baseBean = Utils.fromJsonObject(NetUtil.getResResult(responseBodyResponse), CartoonTeacherWorkListDataBean.class);
                CustomNetCallBack callBack = new CustomNetCallBack(getContext(), baseBean);
                callBack.setCallBack(new CustomNetCallBack.NetCallBack<CartoonTeacherWorkListDataBean>() {
                    @Override
                    public void success(CartoonTeacherWorkListDataBean dataBean) {
                        cartoonTeacherClassDetailAdapter.notifyDataSetChanged(dataBean.getResult());

                        work_num.setText("作业列表（" + dataBean.getResult().size() + "）");

                        stuNum = dataBean.getCount();
                    }

                    @Override
                    public void fail(String errorCode) {


                    }
                }).dealData();
            }

        });
    }
}
